<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模态框拖拽</title>
    <style>
        h2 {
            display: block;
            margin: 0 auto;
            text-align: center;
        }
        
        h2 a {
            text-decoration: none;
            color: #000000;
        }
        
        .login-bg {
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
            background: rgba(0, 0, 0, .3);
            display: none;
        }
        
        .login {
            width: 512px;
            height: 280px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            box-shadow: 0px 0px 20px #ccc;
            box-sizing: border-box;
            padding: 0 10px;
            display: none;
        }
        
        .login-title {
            line-height: 60px;
            text-align: center;
            font-size: 18px;
            cursor: move;
        }
        
        .login-input label {
            display: inline-block;
            width: 20%;
            text-align: right;
        }
        
        .login-input input {
            margin-left: 10px;
            width: 70%;
            outline: none;
            border: 1px solid #ccc;
            line-height: 35px;
            text-indent: 8px;
            margin-bottom: 20px;
        }
        
        .submit {
            display: block;
            width: 50%;
            margin: 30px auto 0;
            line-height: 40px;
            text-decoration: none;
            color: #000000;
            text-align: center;
            border: 1px solid #ccc;
        }
        
        .close {
            width: 40px;
            line-height: 40px;
            position: absolute;
            top: -15px;
            right: -15px;
            text-align: center;
            border: 1px solid #ccc;
            box-shadow: 0px 0px 20px #ccc;
            border-radius: 50%;
            background: white;
            font-size: 14px;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <h2><a href="javascript:;" class="link">点击弹出登录框</a></h2>
    <div class="login-bg"></div>
    <div class="login">
        <div class="login-title">会员登录</div>
        <div class="login-input">
            <label for="uname">用户名：</label>
            <input type="text" id='uname' name="uname" placeholder="请输入用户名">
        </div>
        <div class="login-input">
            <label for="pwd">密码：</label>
            <input type="text" id='pwd' name="pwd" placeholder="请输入密码">
        </div>
        <a href="javascript:;" class="submit">登录会员</a>
        <span class="close">关闭</span>
    </div>
    <script>
        const link = document.querySelector('.link')
        const login = document.querySelector('.login')
        const title = document.querySelector('.login-title')
        const login_bg = document.querySelector('.login-bg')
        const close = document.querySelector('.close')
        link.onclick = () => {
            login.style.display = 'block'
            login_bg.style.display = 'block'
        }
        close.onclick = () => {
            login.style.display = 'none'
            login_bg.style.display = 'none'
        }
        let x = y = 0
        title.addEventListener('mousedown', (e) => {
            console.log('鼠标按下');
            // 盒子内坐标
            x = e.pageX - login.offsetLeft
            y = e.pageY - login.offsetTop;
            // console.log(`x=${x}, y=${y}`);
            document.addEventListener('mousemove', moveEvent)
        })
        title.addEventListener('selectstart', function(e) {
            e.preventDefault();
        })

        document.addEventListener('mouseup', (e) => {
            document.removeEventListener('mousemove', moveEvent)
        })

        function moveEvent(e) {
            // console.log(`mousemove: x=${e.pageX - x}, y=${e.pageY - y}`);
            login.style.left = e.pageX - x + 'px'
            login.style.top = e.pageY - y + 'px'
        }
    </script>
</body>

</html>